



#delimit;
set more off;

cd "C:\....\Replication files";
	
use "ENMI - replication.dta", clear;


#delimit;
xtreg pvote
c.ensh_mi##c.GDPgrowth##1.moderate
majelect  
pvote_l
if inc_cab_l==1, fe robust  ;

estimates store ml_onlyincumbents_fe ;


testnl 
 _b[c.ensh_mi] +   _b[1.moderate#c.ensh_mi] + _b[c.ensh_mi#c.GDPgrowth]
+  _b[1.moderate#c.ensh_mi#c.GDPgrowth]=0;


#delimit;

xtreg pvote
c.ensh_mi##c.GDPgrowth##1.moderate
majelect  
pvote_l
if inc_cab_l==0, fe robust  ;

estimates store  ml_onlyopposition_fe ;
testnl 
 _b[c.ensh_mi] +   _b[1.moderate#c.ensh_mi] + _b[c.ensh_mi#c.GDPgrowth]
+  _b[1.moderate#c.ensh_mi#c.GDPgrowth]=0;


	 #delimit;
xtreg pvote
c.ensh_mi##c.GDPgrowth##1.moderate##1.inc_cab_l
majelect  
pvote_l
, fe robust  ;

  estimates store  ml_full_fe ;
  
/*Wald tests of joint significance.*/
testnl _b[c.ensh_mi#1.inc_cab_l] 
  + _b[c.ensh_mi]==0;

testnl _b[c.ensh_mi#1.inc_cab_l] 
+ _b[c.ensh_mi]== 
_b[c.ensh_mi]==0;

testnl _b[c.ensh_mi]==0 ;

testnl 
 _b[c.ensh_mi] + _b[c.ensh_mi#1.inc_cab_l] +  _b[1.moderate#c.ensh_mi] + _b[c.ensh_mi#c.GDPgrowth]
+  _b[1.moderate#c.ensh_mi#c.GDPgrowth] + _b[1.moderate#1.inc_cab_l#c.ensh_mi#c.GDPgrowth]
+ _b[1.moderate#1.inc_cab_l#c.ensh_mi] + _b[c.ensh_mi#1.inc_cab_l#c.GDPgrowth]= 

 _b[c.ensh_mi] +  _b[1.moderate#c.ensh_mi] + _b[c.ensh_mi#c.GDPgrowth]
+  _b[1.moderate#c.ensh_mi#c.GDPgrowth];


		mat list e(b);
		mat def b = e(b);
#delimit;
 esttab ml_onlyopposition_fe ml_onlyincumbents_fe  ml_full_fe  
	using "figures\Table A11 - four way interaction.rtf", replace 
		b (%10.3f) se scalars("aic AIC" "bic BIC" "rmse Root Mean Squared Error" "chi2 \u0935?{\super 2}" "ll Log-Likelihood" ) 
		obslast star(+ 0.10 * 0.05 ** 0.01 *** 0.001)
		label mtitles (  "Opposition" "Incumbents" "Full") nogaps
title (\label{})
		
		
order(ensh_mi 
1.moderate 
GDPgrowth 
1.moderate#ensh_mi
ensh_mi#GDPgrowth
1.moderate#GDPgrowth
1.moderate#ensh_mi#GDPgrowth
1.moderate#1.inc_cab_l#ensh_mi
1.moderate#1.inc_cab_l#GDPgrowth#ensh_mi
1.moderate#1.inc_cab_l#GDPgrowth
1.moderate#1.inc_cab_l 
1.inc_cab_l 
majelect pvote_l _cons)
keep (ensh_mi 
1.moderate 
GDPgrowth 
1.moderate#ensh_mi
ensh_mi#GDPgrowth
1.moderate#GDPgrowth
1.moderate#ensh_mi#GDPgrowth
1.moderate#1.inc_cab_l#ensh_mi
1.moderate#1.inc_cab_l#GDPgrowth#ensh_mi
1.moderate#1.inc_cab_l#GDPgrowth
1.moderate#1.inc_cab_l 
1.inc_cab_l 
majelect pvote_l _cons)

 
coeflabels(
ensh_mi "ENMI"  
GDPgrowth "GDP Growth Rate" 
1.moderate "Moderated Position"
1.inc_cab_l "Incumbent"
1.inc_cab_l#c.ensh_mi "Incumbent X ENMI"
c.moderate#1.inc_cab_l "Incumbent X Moderate"
1.moderate#c.ensh_mi "ENMI X Moderated"
c.ensh_mi#c.GDPgrowth "ENMI X GDP growth"
1.moderate#c.ensh_mi#c.GDPgrowth "ENMI X Moderated X GDP growth"
1.moderate#c.GDPgrowth "Moderated X GDP growth"
1.moderate#inc_cab_l#c.GDPgrowth"Incumbent X Moderated X GDP growth "
1.moderate#1.inc_cab_l#c.ensh_mi "Incumbent X ENMI X Moderated"


1.moderate#1.inc_cab_l#c.ensh_mi#c.GDPgrowth "Incumbent X ENMI X Moderated X GDP growth"
1.inc_cab_l#c.GDPgrowth "Incumbent X GDP Growth Rate"
 1.moderate#1.inc_cab_l  "Incumbent X Moderated Position"

 majelect "Majoritarian Election"
pvote_l "% Vote{\sub t-1}"
_cons "Constant");




		mat list e(b);
		mat def b = e(b);
		mat list e(V);
		mat def V = e(V);
		
		keep if e(sample);
		

	
		collapse (mean)  pvote
	 pvote_l GDPgrowth
	 mean_ensh_mi=ensh_mi
(sd) sd_ensh_mi=ensh_mi GDPgrowth_sd= GDPgrowth
		(max) max_ensh_mi=ensh_mi
		(min) min_ensh_mi=ensh_mi;
		
		expand 100;
		
		gen enmi=  min_ensh_mi +  (( max_ensh_mi)-( min_ensh_mi))*_n/_N;
		gen GDPgrowth_lo = GDPgrowth-GDPgrowth_sd;
			gen GDPgrowth_hi = GDPgrowth+ GDPgrowth_sd;
save figures\enmi.dta, emptyok replace;
	 /*save dataset	*/


	 
	 
	 
	
program drop _all;

	program define enmi,;
	
		syntax,;
	use   figures\enmi.dta, clear;
	
	drawnorm b1-b18, means(b) cov(V);
		
		gen enmi_gov_logdp= b1*enmi + b2*GDPgrowth_lo + b3*GDPgrowth_lo*enmi
		+ b4*1 + b5*1*enmi + b6*1*GDPgrowth_lo + b7*0*GDPgrowth_lo*enmi 
		+ b8*1 + b12*1*1 + b13*1*1*enmi + b14*1*1*GDPgrowth_lo + 
		b15*1*1*enmi*GDPgrowth_lo + b16*0 + b17* pvote_l + b18;
		
		gen enmi_opp_logdp= b1*enmi + b2*GDPgrowth_lo + b3*GDPgrowth_lo*enmi
		+ b4*1 + b5*1*enmi + b6*1*GDPgrowth_lo + b7*0*GDPgrowth_lo*enmi 
		+ b8*0 + b12*0*0 + b13*0*0*enmi + b14*0*0*GDPgrowth_lo + 
		b15*0*0*enmi*GDPgrowth_lo + b16*0 + b17* pvote_l + b18;
		
				gen enmi_gov_higdp= b1*enmi + b2*GDPgrowth_hi + b3*GDPgrowth_hi*enmi
		+ b4*1 + b5*1*enmi + b6*1*GDPgrowth_hi + b7*0*GDPgrowth_hi*enmi 
		+ b8*1 + b12*1*1 + b13*1*1*enmi + b14*1*1*GDPgrowth_hi + 
		b15*1*1*enmi*GDPgrowth_hi + b16*0 + b17* pvote_l + b18;
		
		gen enmi_opp_higdp= b1*enmi + b2*GDPgrowth_hi + b3*GDPgrowth_hi*enmi
		+ b4*1 + b5*1*enmi + b6*1*GDPgrowth_hi + b7*0*GDPgrowth_hi*enmi 
		+ b8*0 + b12*0*0 + b13*0*0*enmi + b14*0*0*GDPgrowth_hi + 
		b15*0*0*enmi*GDPgrowth_hi + b16*0 + b17* pvote_l + b18;
		
		
		gen sd_enmi_gov= (b1*(mean_ensh_mi + sd_ensh_mi) + b3*1 +b5*1*(mean_ensh_mi+sd_ensh_mi) + b6*pvote_l + b7*GDPgrowth +b9*1*GDPgrowth
			+ b11*0 + b15*1*0 + b16*0 + b17) - (b1*(mean_ensh_mi-sd_ensh_mi) + b3*1 +b5*1*(mean_ensh_mi-sd_ensh_mi) + b6*pvote_l + b7*GDPgrowth +b9*1*GDPgrowth
			+ b11*0 + b15*1*0 + b16*0 + b17 );

		
	append using figures\results.dta;
	save figures\results.dta, replace;
	
	
	
end;

clear;

	save figures\results.dta, emptyok replace; /*save empty dataset for results*/

/*run loop to perform program at multiple differences*/

set more off;
	use figures\enmi.dta, clear	;

	
		simulate, reps(100) : enmi,   ;




/***The loop below opens up the dataset of results and performs _pctile across the range of time for each value of delta, producing the 95% CIs***/
#delimit ;

use results.dta, clear;

foreach var of varlist enmi_gov_logdp enmi_opp_logdp enmi_gov_higdp enmi_opp_higdp sd_enmi_gov {;
/*D_p05_pct*/
statsby `var'_lo=r(r1) `var'_median=r(r2) `var'_hi=r(r3), by(enmi)
saving(figures\CIs_`var'.dta, replace): _pctile `var', p(2.5, 50, 97.5);
}
;

	
	/*define local containing names of all but first variable*/
	#delimit;
	set more off;
use figures\CIs_enmi_gov_logdp.dta, clear; 	/*open dataset of CIs for first variable*/
	append using figures\CIs_enmi_opp_logdp.dta ;
		append using figures\CIs_enmi_gov_higdp.dta;
	append using figures\CIs_enmi_opp_higdp.dta ;
	
	append using figures\CIs_sd_enmi_gov.dta ;
	append using figures\enmi.dta;
	
	
	
	gen xtick=10.1;
	gen xtick2=10.05;

#delimit;
twoway  lowess  enmi_gov_logdp_median enmi if enmi<40, sort scheme(s1mono) lcolor(cranberry) 
		||  lowess enmi_gov_logdp_lo  enmi if enmi<40, lpattern(".") lcolor(red) lwidth(medium)
		|| lowess enmi_gov_logdp_hi  enmi if enmi<40, lpattern(".")  lcolor(red) lwidth(medium)
		|| lowess  enmi_opp_logdp_median enmi if enmi<40, sort  lcolor(blue*.3) lpattern(-.)
		||  lowess enmi_opp_logdp_lo  enmi if enmi<40, lpattern("-") lcolor(dknavy*.3)
		|| lowess enmi_opp_logdp_hi  enmi if enmi<40, lpattern("-")  lcolor(dknavy*.3)


			  title("Predicted difference in % Vote" "from ENMI")
			  subtitle("low GDP growth")
			  ytitle("Difference in % Vote") 
			  xtitle("ENMI") xtick() xlabel() 
			legend(order(1 "Government" 4 "Opposition"))  saving(figures\logdp.gph, replace) ;
	
	#delimit;
twoway  lowess  enmi_gov_higdp_median enmi if enmi<40, sort scheme(s1mono) lcolor(cranberry) 
		||  lowess enmi_gov_higdp_lo  enmi if enmi<40, lpattern(".") lcolor(red) lwidth(medium)
		|| lowess enmi_gov_higdp_hi  enmi if enmi<40, lpattern(".")  lcolor(red) lwidth(medium)
		|| lowess  enmi_opp_higdp_median enmi if enmi<40, sort  lcolor(blue*.3) lpattern(-.)
		||  lowess enmi_opp_higdp_lo  enmi if enmi<40, lpattern("-") lcolor(dknavy*.3)
		|| lowess enmi_opp_higdp_hi  enmi if enmi<40, lpattern("-")  lcolor(dknavy*.3)


			  title("Predicted difference in % Vote" "from ENMI")
			  subtitle("High GDP growth")
			  ytitle("Difference in % Vote") 
			  xtitle("ENMI") xtick() xlabel() 
			legend(order(1 "Government" 4 "Opposition"))saving(figures\higdp.gph, replace)   ;
	

		graph combine figures\logdp.gph figures\higdp.gph, ycommon scheme(s1mono);
		
sum sd_enmi_gov*;
	
	egen min_ensh_mi= min(ensh_mi);
	egen max_ensh_mi= max(ensh_mi);
	egen mean_sd_enmi_gov_median= mean(sd_enmi_gov_median);
	
	gen per_change= mean_sd_enmi_gov_median/ (abs(min_ensh_mi - max_ensh_mi));
sum per_change;
